<template>
	<view v-if="display">
		<view class="cash_box">
			<view class="cash_quota">
				<text>提现金额(元)</text>
				<view class="quota">
					<view class="">
						<h1>￥</h1>
						<input type="number" v-model="amount" placeholder="输入提现金额" @input="_input">
					</view>
					<text @click="whole">全部</text>
				</view>
				<view class="withdrawal_money">可提现金额(元) <text>{{money}}</text></view>
			</view>
			<view class="cash_card" style="display: none;">
				<text>提现到账户</text>
				<view class="card">
					<view class="lefts">
						<img :src="data.bank_logo" alt="">
						<h5>{{data.bank_name}}({{bank_card_number}})</h5>
					</view>
					<view class="rights" @click="replace_card">
						<text>更换</text>
					</view>
				</view>
			</view>
			<view class="account">
				<view class="title">
					<text>提现到账户</text>
				</view>
				<view class="lists">
					<radio-group @change="accountChange">
						<label v-for="(item, index) in bankList" :key="item.id" class="items">
							<view class="checkboxs">
								<radio :value="item.id" activeBackgroundColor="#19be6b"
									:checked="bank_id === item.id" />
							</view>
							<view class="accountInfo">
								<view class="accountName">
									<text>{{item.name}}</text>
									<text>{{item.phone}}</text>
								</view>
								<view class="accountBank">
									<img :src="item.bank_logo" alt="">
									<h5>(尾号{{item.card_number}})</h5>
								</view>
							</view>
							<view class="operates">
								<u-tag text="更换" plain size="mini" type="warning" @click="replace_card"></u-tag>
							</view>
						</label>
					</radio-group>
				</view>
				<!-- <view class="addBtn" @click="bankAdd">
					<u-icon name="plus" color="#ffffff"></u-icon>
					<text>添加账户</text>
				</view> -->
			</view>
			<view class="cash_Tips">
				<text>温馨提示：</text>
				<!-- <text>1、结算方式：{{status.clearing_form == 1?'秒结算':'月结算'}}</text> -->
				<text>1、单次提现最低{{status.withdrawal_min_money}}元，最高{{status.withdrawal_max_money}}元</text>
				<text>2、提现时间：工作日【{{status.withdrawal_start_time}}-{{status.withdrawal_end_time}}】</text>
				<text>3、手续费：{{status.service_charge}},税率(%)：{{status.tax_rate}}</text>
				<text>4、单日提现次数：{{status.withdrawal_number}}</text>
				<text v-if="status.remarks != '' && status.remarks != null">备注信息：{{status.remarks}}</text>
			</view>
			<view class="order" v-if="status.is_open == 1&&amount != ''" @click="verify">提现</view>
			<view class="order" style="background-color: #eee;" v-else>提现</view>
			<u-modal :show="withdrawShow" title="提现确认" @cancel="withdrawShow = false" @close="withdrawShow = false"
				@confirm="withdrawConfirm" showCancelButton>
				<view class="slot-content">
					<view class="tx_content">
						<text>你确定往</text>
						<text class="tx_bank_info">{{bankItem.bank_name}}(尾号{{bankItem.card_number}})</text>
						<text>中提现</text>
						<text class="tx_bank_info">{{amount}}</text>
						<text>元？</text>
					</view>
					<view class="tx_msg">
						<text>您该笔提现可能需要纳税，预估金额非实际纳税金额，请以实际为准!</text>
					</view>
					<view class="tx_title">
						<text>提现明细</text>
					</view>
					<view class="tx_detail">
						<view class="tx_item">
							<view class="tags">
								<text>提现金额</text>
							</view>
							<view class="values">
								<text>￥</text>
								<text>{{amount}}</text>
							</view>
						</view>
						<view class="tx_item">
							<view class="tags">
								<text>服务费税费</text>
							</view>
							<view class="values">
								<text>￥</text>
								<text>{{serviceFee}}</text>
							</view>
						</view>
						<view class="tx_item">
							<view class="tags">
								<text>手续费</text>
							</view>
							<view class="values">
								<text>￥</text>
								<text>{{fee}}</text>
							</view>
						</view>
					</view>
					<view class="tx_title">
						<text>个税明细</text>
					</view>
					<view class="tx_detail">
						<view class="tx_item">
							<view class="tags">
								<text>预计个人所得税</text>
							</view>
							<view class="values">
								<text>￥</text>
								<text>{{tax.taxPayable}}</text>
							</view>
						</view>
						<view class="tx_item">
							<view class="tags">
								<text>预计增值税</text>
							</view>
							<view class="values">
								<text>￥</text>
								<text>{{tax.estimatedValeAddedTax}}</text>
							</view>
						</view>
						<view class="tx_item">
							<view class="tags">
								<text>预计附加税</text>
							</view>
							<view class="values">
								<text>￥</text>
								<text>{{tax.estimatedSurtax}}</text>
							</view>
						</view>
					</view>
					<view class="tx_total">
						<view class="tags">
							<text>预计到账金额</text>
						</view>
						<view class="values">
							<text>￥</text>
							<text>{{tax.actualAmount}}</text>
						</view>
					</view>
				</view>
			</u-modal>
		</view>
	</view>
</template>

<script>
	import {
		data
	} from '../../../../uni_modules/uview-ui/libs/mixin/mixin'
	export default {
		data() {
			return {
				card: '',
				data: {},
				money: 0,
				bank_card_number: '',
				display: false,
				amount: '0.00',
				serviceFee: '0.00',
				fee: '0.00',
				type: '',
				balance_type_id: null,
				status: {},
				bank_id: null,
				bankList: [],
				bankItem: {
					id: 0,
					bank_name: '',
					card_number: '',
					bank_card_number: '',
				},
				withdrawShow: false,
				tax: {
					taxPayable: '0.00', // 应缴个税
					taxDeduction: '0.00', // 减免个税
					actualTaxPayment: '0.00', // 实扣个税
					actualAmount: '0.00', // 实到金额
					estimatedValeAddedTax: '0.00', // 预估增值税
					estimatedSurtax: '0.00', // 预估附加税
				},
			}
		},
		onNavigationBarButtonTap() {
			uni.navigateTo({
				url: '/pages/mine/profit/Cash/Cash_info'
			})
		},
		onShow() {
			this._cash()
			this._status()
			this.getBalance()
			this.getBankList()
		},
		methods: {
			//获取提现数据
			async _cash() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'Bankcard/getBankCardDetail',
					data: {},
				});
				uni.hideLoading()
				if (res.code == 200) {
					if (res.data !== null) {
						this.display = true
						this.data = res.data
						this.bank_card_number = this.data.bank_card_number.slice(-4)
					} else {
						let _this = this
						uni.showModal({
							title: '提示',
							content: '未绑定银行卡，是否绑定银行卡',
							success: function(ress) {
								if (ress.confirm) {
									_this.bank_card()
								} else if (ress.cancel) {
									uni.navigateBack({});
								}
							}
						});
					}
				}
			},
			//银行卡管理
			bank_card() {
				uni.navigateTo({
					url: '/pages/mine/profit/bank_card/bank_card'
				})
			},
			//获取提现状态
			async _status() {
				const {
					data: res
				} = await this.$myHttp({
					url: 'Withdraw/getWithdrawStateNew'
				});
				if (res.code == 200) {
					this.fee = res.data.service_charge
					this.status = res.data
				}
			},
			//获取余额
			async getBalance() {
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/getHzyBalance'
				});
				if (res.code == 200) {
					this.money = res.data
				}
			},
			// 获取账号
			async getBankList() {
				const {
					data: res
				} = await this.$myHttp({
					url: 'bankcard/getBankList'
				});
				if (res.code == 200) {
					this.bankList = res.data.map((item) => {
						item.id = item.id.toString()
						item.card_number = item.bank_card_number.slice(-4)
						return item
					})
					this.bankItem = res.data[0]
					this.bank_id = res.data[0].id
				}
			},
			//全部
			whole() {
				this.amount = this.money
			},
			//验证提现
			verify() {
				if (parseFloat(this.amount) >= parseFloat(this.status.withdrawal_min_money)) {
					var time = new Date()
					let hour = time.getHours() < 10 ? "0" + time.getHours() : time.getHours()
					let minute = time.getMinutes() < 10 ? "0" + time.getMinutes() : time.getMinutes()
					let _time = hour + ':' + minute
					if (_time > this.status.withdrawal_start_time && _time < this.status.withdrawal_end_time) {
						this.serviceFee = (this.amount*100*this.status.tax_rate/10000).toFixed(2)
						this.queryTax()
					} else {
						uni.showToast({
							title: '不在提现时间范围内',
							icon: 'none',
							duration: 2000
						})
					}
				} else {
					uni.showToast({
						title: '单次提现最低' + this.status.withdrawal_min_money + '元',
						icon: 'none',
						duration: 2000
					})
				}
			},
			// 税费预估查询
			async queryTax() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'Withdraw/queryTax',
					data: {
						idCard: this.bankItem.idcards,
						amount: this.amount
					},
				});
				uni.hideLoading()
				if (res.code == 200) {
					if (res.data.taxPayable) {
						this.tax = res.data
					}
					this.tax.taxPayable = (this.tax.taxPayable/100).toFixed(2)
					this.tax.estimatedValeAddedTax = (this.tax.estimatedValeAddedTax/100).toFixed(2)
					this.tax.estimatedSurtax = (this.tax.estimatedSurtax/100).toFixed(2)
					this.tax.actualAmount = (this.amount*100 - this.serviceFee*100 - this.fee*100 - this.tax.taxPayable*100 - this.tax.estimatedValeAddedTax*100 - this.tax.estimatedSurtax*100) / 100
					this.withdrawShow = true
				}
			},
			_input() {
				if (parseFloat(this.amount) > parseFloat(this.money)) {
					uni.showToast({
						title: '超出提现范围',
						icon: 'none',
						duration: 2000
					})
					this.amount = ''
				}
			},
			//点击提现
			async Cash() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'Withdraw/withdrawCashs',
					data: {
						balance_type_id: this.balance_type_id,
						withdrawal_money: this.amount,
						balance_types: this.type,
						bank_id: this.bank_id
					}
				});
				uni.hideLoading()
				if (res.code == 200) {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 3000,
						success() {
							setTimeout(function() {
								uni.navigateBack({
									delta: 2
								})
							}, 1500)
						}
					})
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 3000,
					})
					this.amount = ''
				}
			},
			replace_card() {
				uni.navigateTo({
					url: '/pages/mine/profit/bank_card/bank_card'
				})
			},
			// 切换账号
			accountChange(evt) {
				console.log(evt)
			},
			// 提现确认
			withdrawConfirm() {
				this.Cash()
			},
			// 添加账户
			bankAdd() {
				uni.navigateTo({
					url: '/pages/mine/bank/add'
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.cash_box {
		margin: 30rpx;
		padding: 30rpx;
		box-sizing: border-box;
		background-color: #fff;

		.cash_card {
			border-top: #eee 1px solid;
			padding: 30rpx 0;
			display: flex;
			flex-direction: column;
			border-bottom: #eee 1px solid;

			.card {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-top: 30rpx;

				.lefts {
					width: 90%;
				}

				.rights {
					display: flex;
					align-items: center;
				}

				img {
					width: 200rpx;
					height: 53rpx;
					margin-right: 30rpx;
					float: left;
				}

				text {
					font-size: 12px;
					line-height: 26px;
					color: #2979ff;
				}
			}

			text {
				font-size: 28rpx;
				color: #333;
			}

			h5 {
				float: left;
				font-size: 38rpx;
			}
		}

		.cash_quota {
			padding-bottom: 30rpx;

			text {
				font-size: 28rpx;
				color: #333;
			}

			.quota {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 30rpx 20rpx;
				border-bottom: #eee 1px solid;

				view {
					display: flex;
					align-items: center;

					input {
						padding: 15rpx;
					}
				}

				text {
					font-size: 32rpx;
					color: #fff;
					padding: 10rpx 40rpx;
					border-radius: 50rpx;
					background-color: #19be6b;
					white-space: nowrap;
				}
			}

			.withdrawal_money {
				padding: 30rpx 0;
				font-size: 26rpx;
				border-bottom: #eee 1px solid;

				text {
					font-weight: bold;
					font-size: 34rpx;
					padding-left: 20rpx;
				}
			}
		}

		.account {
			.lists {
				.items {
					padding: 30rpx 0;
					display: flex;
					align-items: center;
					border-bottom: #eee 1px solid;

					.checkboxs {
						width: 10%;
					}

					.accountInfo {
						width: 75%;

						.accountName {
							border-bottom: 1px dashed #f4f4f5;
							font-size: 26rpx;
							color: #82848a;
							padding-bottom: 10rpx;
							padding-left: 20rpx;

							text {
								padding-right: 20rpx;
							}
						}

						.accountBank {
							display: flex;
							align-items: center;

							img {
								width: 160rpx;
							}

							h5 {
								margin-left: 16rpx;
							}
						}
					}

					.operates {
						width: 15%;
						display: flex;
						justify-content: flex-end;
						font-size: 24rpx;
					}
				}
			}

			.addBtn {
				margin-top: 30rpx;
				font-size: 26rpx;
				background-color: #2979ff;
				color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 10rpx;
				padding: 20rpx 0;

				text {
					padding-left: 10rpx;
				}
			}
		}

		.cash_Tips {
			display: flex;
			flex-direction: column;
			margin-top: 30rpx;
			font-size: 24rpx;
			background-color: #f4f4f5;
			padding: 20rpx;
			color: #909399;

			text {
				margin-bottom: 10rpx;
			}
		}

		.order {
			background-color: #19be6b;
			width: 90%;
			margin: 50rpx auto;
			text-align: center;
			padding: 30rpx 0;
			border-radius: 50rpx;
			color: #fff;
			transition: all 0.5s;
		}

		.order:active {
			background-color: #75c3c5;
		}
	}

	.tx_content {
		font-size: 26rpx;

		.tx_bank_info {
			padding: 0 10rpx;
			color: #2979ff;
		}
	}

	.tx_msg {
		margin-top: 10rpx;
		font-size: 26rpx;
		color: #ff9900;
	}

	.tx_title {
		font-size: 30rpx;
		font-weight: bold;
		padding: 10rpx 0;
		border-bottom: 1px solid #eee;
	}

	.tx_detail {
		font-size: 26rpx;

		.tx_item {
			padding: 10rpx 0;
			display: flex;
			justify-content: space-between;
			border-bottom: 1px dashed #f4f4f5;
		}
	}
	
	.tx_total {
		font-size: 30rpx;
		font-weight: bold;
		border-top: 1px solid #eee;
		display: flex;
		justify-content: space-between;
		padding-top: 20rpx;
	}
</style>